package fr.upstlse.glre.client.administration.utilisateurs.action;

import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.FieldEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.FormPanel;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.espacededie.component.IUserModification;
import fr.upstlse.glre.client.widgets.fields.EtudiantFields;
import fr.upstlse.glre.client.widgets.fields.ProfesseurFields;

public class RoleSelectionAction implements Listener<FieldEvent> {

	FormPanel formPanel;
	IUserModification iuModification;
	
	
	public RoleSelectionAction(IUserModification pIUserModification) {
		super();
		iuModification = pIUserModification;
		this.formPanel = pIUserModification.getFormPanel();
	}


	public void handleEvent(FieldEvent be) {
		ComboBox lRoleComboBox = (ComboBox) be.field;
		ModelData lRoleModelData = lRoleComboBox.getValue();
		String lRoleString = (String) lRoleModelData.get(RfgConstante.ROLE_MODELDATA_ROLE);
		
		if(iuModification.getAdditionalFields() != null) {
			iuModification.getAdditionalFields().removeFields();
			iuModification.setAdditionalFields(null);
		}
		if(lRoleString == "ETUDIANT") {
			iuModification.setAdditionalFields(new EtudiantFields(null, formPanel, false));
		}
		else if(lRoleString == "PROFESSEUR") {
			iuModification.setAdditionalFields(new ProfesseurFields(null, formPanel, false));
		}
		
		formPanel.layout();
	}

}
